package com.chengxiaoxiao.utils.db;

import javax.sql.DataSource;
import java.sql.*;

public class DBAssist {

    private DataSource dataSource;

    public DBAssist(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    /**
     * 执行增删改语句
     * @param sql
     * @param pms
     */
    public void update(String sql, Object... pms) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = dataSource.getConnection();
            ps = conn.prepareStatement(sql);
            //设置参数
            ParameterMetaData pmd = ps.getParameterMetaData();
            int num = pmd.getParameterCount();
            if (num > 0) {
                if (pms == null) {
                    throw new IllegalArgumentException("有占位符但是没有传入参数");
                }
                if (num != pms.length) {
                    throw new IllegalArgumentException("参数个数不匹配");
                }
                for (int i = 0; i < num; i++) {
                    ps.setObject(i + 1, pms[i]);
                }
            }
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            release(conn, ps, rs);
        }
    }

    /**
     * 执行查询方法
     *
     * @param sql
     * @param handler
     * @param pms
     * @return
     */
    public Object query(String sql, ResultSetHandler handler, Object... pms) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = dataSource.getConnection();
            ps = conn.prepareStatement(sql);

            ParameterMetaData pmd = ps.getParameterMetaData();
            int num = pmd.getParameterCount();
            if (num > 0) {
                if (pms == null) {
                    throw new IllegalArgumentException("有占位符但是没有传入参数");
                }
                if (num != pms.length) {
                    throw new IllegalArgumentException("参数个数不匹配");
                }

                for (int i = 0; i < num; i++) {
                    ps.setObject(i + 1, pms[i]);
                }
            }
            rs = ps.executeQuery();
            return handler.handle(rs);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            release(conn, ps, rs);
        }
    }

    /**
     * 关闭链接
     *
     * @param conn
     * @param ps
     * @param rs
     */
    private void release(Connection conn, PreparedStatement ps, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if (ps != null) {
            try {

                ps.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            ps = null;
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            conn = null;
        }


    }
}
